// pages/register/register.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: { vuUserId: '', vuUserName: '', vuPassword: '', checkpwd: '', checkuid: false }
  },
  change(e: any) {
    let user = this.data.user;
    if (e.currentTarget.dataset.type === 'id')
      user.vuUserId = e.detail.value
    else if (e.currentTarget.dataset.type === 'pwd')
      user.vuPassword = e.detail.value
    else if (e.currentTarget.dataset.type === 'cpwd')
      user.checkpwd = e.detail.value
    else if (e.currentTarget.dataset.type === 'name')
      user.vuUserName = e.detail.value
    this.setData({ user });
  },
  chuserId() {
    let ithis = this;
    wx.request({
      url: 'http://localhost:9090/vote-user/userIdUN',
      method: 'GET',
      data: { userId: this.data.user.vuUserId },
      success(res: any) {
        ithis.data.user.checkuid = res.data;
        if (res.data) ithis.msg('id重复')
      }
    });
  },
  msg(text: string) { wx.showToast({ title: text, icon: 'none' }); },
  register() {
    if (!this.data.user.vuUserId || !this.data.user.vuUserName || !this.data.user.vuPassword || !this.data.user.checkpwd) {
      this.msg('所有字段均不能为空');
      return;
    }
    if (this.data.user.vuPassword !== this.data.user.checkpwd) {
      this.msg('两次输入的密码不一致');
      return;
    }
    if (this.data.user.checkuid) {
      this.msg('id重复');
      return;
    }
    wx.request({
      url: 'http://localhost:9090/vote-user/userRegister',
      method: 'GET',
      data: this.data.user,
      success(res: any) {
        if (res.data) {
          wx.showToast({
            title: '注册成功！！！',
            icon: 'success',
            duration: 1000
          });
          setTimeout(() => {
            wx.navigateTo({ url: '../login/login' })
          }, 1000)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})